package com.mall.order.controller;

import com.ctg.mse.common.model.error.CommonCode;
import com.ctg.mse.common.model.result.ResultGeneralModel;
import com.mall.order.dao.entity.TOrder;
import com.mall.order.service.OrderService;
import com.mall.order.vo.CreateOrderInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: OrderController 订单服务，对前端开启的
 * @Author: zhouzh
 * @CreateTime: 2022-10-21  11:34
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private final static Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;

    @PostMapping("/createOrder")
    public ResultGeneralModel<TOrder> createOrder(@RequestBody CreateOrderInfo createOrderInfo) {

        LOGGER.info("创建订单请求/createOrder  createOrderInfo :" + createOrderInfo);

        TOrder order;
        try{
            //参数校验
            if (createOrderInfo == null || createOrderInfo.getPrice() == null || createOrderInfo.getProductInfo() == null ||
                    createOrderInfo.getProductInfo().size() == 0) {
                return ResultGeneralModel.newFail(CommonCode.ILLEGAL_PARAM, "create order info is null!");
            }

            order = orderService.createOrder(createOrderInfo.getPrice(), createOrderInfo.getProductInfo());
        }catch(Exception e){
            LOGGER.error("",e);
            throw e;
        }

        return ResultGeneralModel.newSuccess(order);
    }




}
